package com.ourstocks.test.addFiles;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.junit.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.MongoClient;
import com.ourstocks.module.maindata.po.StockCodeInfo;
import com.ourstocks.module.maindata.po.StockHisRoe;

public class AddFile {
    private static final String MONGODBHOST = "127.0.0.1";
    private static final int MONGODBPORT = 27017;
    private static final String DATABASENAME = "westock";
    private static final MongoTemplate mongoTemplate = new MongoTemplate(new MongoClient(MONGODBHOST, MONGODBPORT), DATABASENAME);
	
    @Test
	public void addFiles() throws IOException {
//		File file = new File("D:/Development Tools/eclipse-workspace/workspace_westocks/data/");
//		if (!file.exists()) {
//			file.mkdir();
//			System.out.println("文件夹已创建");
//		}
		List<StockCodeInfo> list = mongoTemplate.find(new Query(), StockCodeInfo.class);
		for (int i = 0; i < list.size(); i++) {
		        //定义文件名格式并创建
//		        File txtFile = File.createTempFile(list.get(i).getCode(), ".xsl", new File("D:/Development Tools/eclipse-workspace/workspace_westocks/data/"));
//		        FileOutputStream fos = new FileOutputStream("src/test/java/io/file.txt");
			if (list.get(i).getCode().matches("\\d{6}")) {
		
					FileOutputStream fos = new FileOutputStream("D:/Development Tools/eclipse-workspace/workspace_westocks/data/" + list.get(i).getCode()+".xsl");
					System.out.println("创建成功！");
			}
		}
	}
    
    /**
     * 清理stock_code_info里面的脏数据
     */
    @Test
    public void cleanDataStockCodeInfo() {
    	List<String> lists = new ArrayList<String>();
    	List<StockCodeInfo> list = mongoTemplate.find(new Query(), StockCodeInfo.class);
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getCode().length() > 6) {
				lists.add(list.get(i).getCode());
			}
		}
		System.out.println(lists);
		lists.stream().forEach(code -> {
			
			mongoTemplate.remove(new Query(Criteria.where("code").is(code)),StockCodeInfo.class);
			
		});
		
    }
	@Test
	public void test() {
		System.out.println("怎么了");
	}
	
}
